package com.projectsexception.festivales.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;

public class Serializator {

    public static byte[] exportObject(Serializable objeto) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        byte[] array = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(objeto);
            array = baos.toByteArray();
        } catch (IOException e) {
            CustomLog.error(Serializator.class.getName(), e.getMessage());
        }
        return array;
    }

    public static Object importObject(byte[] array) {
        ByteArrayInputStream bais = new ByteArrayInputStream(array);
        ObjectInputStream ois;
        Object o = null;
        try {
            ois = new ObjectInputStream(bais);
            o = ois.readObject();
        } catch (StreamCorruptedException e) {
            CustomLog.error(Serializator.class.getName(), e.getMessage());
        } catch (IOException e) {
            CustomLog.error(Serializator.class.getName(), e.getMessage());
        } catch (ClassNotFoundException e) {
            CustomLog.error(Serializator.class.getName(), e.getMessage());
        }
        return o;
    }

}
